package com.baobao.service;

import com.baobao.dao.StudentDAO;
import com.baobao.domain.Student;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Student entities
 * 
 */

@Service("StudentService")
@Transactional
public class StudentServiceImpl implements StudentService {

	/**
	 * DAO injected by Spring that manages Student entities
	 * 
	 */
	@Autowired
	private StudentDAO studentDAO;

	/**
	 * Instantiates a new StudentServiceImpl.
	 *
	 */
	public StudentServiceImpl() {
	}

	/**
	 * Delete an existing Student entity
	 * 
	 */
	@Transactional
	public void deleteStudent(Student student) {
		studentDAO.remove(student);
		studentDAO.flush();
	}

	/**
	 * Return all Student entity
	 * 
	 */
	@Transactional
	public List<Student> findAllStudents(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Student>(studentDAO.findAllStudents(startResult, maxRows));
	}

	/**
	 */
	@Transactional
	public Student findStudentByPrimaryKey(String id) {
		return studentDAO.findStudentByPrimaryKey(id);
	}

	/**
	 * Save an existing Student entity
	 * 
	 */
	@Transactional
	public void saveStudent(Student student) {
		Student existingStudent = studentDAO.findStudentByPrimaryKey(student.getId());

		if (existingStudent != null) {
			if (existingStudent != student) {
				existingStudent.setId(student.getId());
				existingStudent.setName(student.getName());
				existingStudent.setClassId(student.getClassId());
				existingStudent.setSex(student.getSex());
				existingStudent.setBbId(student.getBbId());
			}
			student = studentDAO.store(existingStudent);
		} else {
			student = studentDAO.store(student);
		}
		studentDAO.flush();
	}

	/**
	 * Return a count of all Student entity
	 * 
	 */
	@Transactional
	public Integer countStudents() {
		return ((Long) studentDAO.createQuerySingleResult("select count(o) from Student o").getSingleResult()).intValue();
	}

	/**
	 * Load an existing Student entity
	 * 
	 */
	@Transactional
	public Set<Student> loadStudents() {
		return studentDAO.findAllStudents();
	}
	
	/**
	 *根据抱抱卡号获取学生
	 * 
	 */
	@Transactional
	public Set<Student> findStudentByBbId(String bbId) {
		return studentDAO.findStudentByBbIdContaining(bbId);
	}
}
